package com.alibaba.citrus.cr.sales.plan.facade.service.setting;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.sales.plan.api.setting.SalesPlanStrategyDetailWriteService;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.PageQuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.QuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.cr.sales.plan.functions.planstrategy.SalesPlanStrategyDetailEntity;
import com.alibaba.citrus.cr.sales.plan.repository.SalesPlanStrategyDetailRepository;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanStrategyDetailSDO;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class SalesPlanStrategyDetailWriteServiceImpl implements SalesPlanStrategyDetailWriteService {

    @Resource
    private SalesPlanStrategyDetailRepository salesPlanStrategyDetailRepository;

    @FacadeInvoker
    @Override
    public Result<String> createSalesPlanStrategyDetail(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO)  {
        // 请求体校验
        checkBizRule(salesPlanStrategyDetailSDO);
        // 去重判断
        checkIfRepeat(salesPlanStrategyDetailSDO);
        // 构造实体
        SalesPlanStrategyDetailEntity planStrategyEntity = SalesPlanStrategyDetailEntity.of(salesPlanStrategyDetailSDO);
        String lastInsertId = planStrategyEntity.createSalesPlanStrategy();
        return Result.success(lastInsertId);
    }

    @FacadeInvoker
    @Override
    public Result<Boolean> editSalesPlanDetail(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO) {
        // 请求体校验
        checkBizRule(salesPlanStrategyDetailSDO);
        SalesPlanStrategyDetailEntity salesPlanStrategyDetailEntity = SalesPlanStrategyDetailEntity.of(salesPlanStrategyDetailSDO);
        return Result.success(salesPlanStrategyDetailEntity.edit());
    }

    @FacadeInvoker
    @Override
    public Result<Boolean> delete(QuerySalesPlanStrategyDetailRequest querySalesPlanStrategyDetailRequest) {
        Boolean deletePlanCustomer = salesPlanStrategyDetailRepository.deleteSalesPlanDetailStrategyById(querySalesPlanStrategyDetailRequest);
        return Result.success(deletePlanCustomer);
    }

    private void checkBizRule(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO) {
        if (null == salesPlanStrategyDetailSDO) {
            throw new FacadeException("OTS-02-004-01-15-022", "请求体不能为空");
        }

        int sum = Integer.sum(Optional.ofNullable(salesPlanStrategyDetailSDO.getFirstWeightRatio()).orElse(0), Optional.ofNullable(salesPlanStrategyDetailSDO.getSecondWeightRatio()).orElse(0));
        if (sum != 10000) {
            throw new FacadeException("OTS-02-004-01-16-021");
        }
    }

    private void checkIfRepeat(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO) {
        PageQuerySalesPlanStrategyDetailRequest request = new PageQuerySalesPlanStrategyDetailRequest();
        request.setReplyStrategyId(salesPlanStrategyDetailSDO.getReplyStrategyId());
        request.setStatus(1);
        request.setReplyStrategyType(salesPlanStrategyDetailSDO.getReplyStrategyType());
        request.setReplyTargetId(salesPlanStrategyDetailSDO.getReplyTargetId());
        request.setStart(0);
        request.setLimit(1);
        PageResult<List<SalesPlanStrategyDetailSDO>> pageResult = salesPlanStrategyDetailRepository.pageQuery(request);
        if (null != pageResult && pageResult.getTotal() > 0) {
            throw new FacadeException(ErrorMessage.code("OTS-02-004-01-16-023"));
        }
    }
}



